package com.ge.med.cchelper.popup.actions;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.ge.med.cchelper.popup.job.BaseJob;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.ReourceHelper;

public class DisConnection implements IObjectActionDelegate{

	public DisConnection(){
		super();
	}
	
	public void run(IAction arg0) {
		
		BaseJob login = new BaseJob("Disconnection") {

			@Override
			protected IStatus start(IProgressMonitor arg0) {
				IProject currentProject = ReourceHelper.getProject();
				String projectId = currentProject.getName();
				ReourceHelper resource = ReourceHelper.getResource();
				resource.removeSession(projectId);
				LogUtil.out("destroy session:"+projectId);
				return  Status.OK_STATUS;
			}
			
		};
		
		login.schedule();
	
		
	}

	public void selectionChanged(IAction arg0, ISelection arg1) {
		if (arg1 != null && arg1 instanceof IStructuredSelection) {
			IStructuredSelection obj = (IStructuredSelection) arg1;
			Object objs = obj.getFirstElement();
					
			if (objs instanceof IProject) {
				IProject currentProject = (IProject) objs;
				ReourceHelper.setProject(currentProject);
				
				ReourceHelper resource = ReourceHelper.getResource();
				
				if(resource.getSession(currentProject.getName()) == null){
					arg0.setEnabled(false);
				}
			}else{
				arg0.setEnabled(false);
			}
		}
		
	}

	public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
		// TODO Auto-generated method stub
		
	}

}
